using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Deepcode.Flux.Core.UI.Controls
{
	/// <summary>
	/// Simply creates a div with padding based on the class being used.
	/// The default class is "pad10" - specify a different one with 
	/// the CssClass property.
	/// </summary>
	[ParseChildren(false)]
	public class ShadePadBox : Deepcode.Flux.Core.UI.Controls.Base.BaseControl
	{
		/// <summary>
		/// This property determines if the shade pad box is rendered as a DIV
		/// or as a TABLE. The default of this property if false, which forces
		/// the shadepadbox control to render a single celled table.
		/// </summary>
		public bool UseDIV
		{
			get
			{
				object ud = ViewState["UseDIV"];
				if( ud == null ) return false;
				return (bool) ud;
			}
			set
			{
				ViewState["UseDIV"] = value;
			}
		}
		/// <summary>
		/// Init - default the cssclass to pad10 and ensure common stylesheets are included
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
			if( CssClass == String.Empty ) CssClass = "pad10shade";

			this.LoadClientStyleSheet("ShadePadBox/ShadePadBox.css", "Deepcode.Flux.Core.UI.Controls.ShadePadBox");
		}
		public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
		{
			if (UseDIV)
			{
				writer.Write("<div class='{0}'>", this.CssClass);
			}
			else
			{
				string hData = "";
				if( this.Height != Unit.Empty ) hData = String.Format("height='{0}'", this.Height.ToString());
				writer.Write("<table width='100%' {1} cellspacing='0' cellpadding='0' border='0'><tr><td class='{0}' valign='top'>", this.CssClass, hData);
			}
		}
		public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
		{
			if (UseDIV)
			{
				writer.Write("</div>");
			}
			else
			{
				writer.Write("</td></tr></table>");
			}
		}
	}
}
